package com.yuerlive.rainbow.module.live.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.entity.DayTaskData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admin on 2017/7/18.
 */

public class TaskAdatpter extends BaseAdapter {
    private Context context;
    private List<DayTaskData> datas;
    private GetListener listener;
    public TaskAdatpter(Context context) {
        this.context=context;
    }
    public void setDatas(List<DayTaskData> datas){
        this.datas=datas;
    }
    public void setListener(GetListener listener){
      this.listener=listener;
    }
    @Override
    public int getCount() {
        if(datas==null || datas.size()==0){
            return  0;
        }
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_day_task,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }
        DayTaskData data=(DayTaskData)datas.get(position);
        holder.tvTaskName.setText(data.getName()+" ("+data.getDoTimes()+"/"+data.getTimes()+")");
        holder.tvDes.setText(data.getDescribed());
        if(data.getIsGgetReward()==0 && data.getDoTimes()==data.getTimes()){
            holder.tvGet.setBackground(context.getResources().getDrawable(R.drawable.bg_retangle_corner_solid_10));
            holder.tvGet.setClickable(true);
            holder.tvGet.setEnabled(true);
        }else{
            holder.tvGet.setBackground(context.getResources().getDrawable(R.drawable.bg_retangle_corner_solid_11));
            holder.tvGet.setClickable(false);
            holder.tvGet.setEnabled(false);
        }
        holder.tvGet.setOnClickListener((view)->{
            listener.getRainbow(data);
        });
        return convertView;
    }
    static class ViewHolder{
        @BindView(R.id.tvTaskName)
        TextView tvTaskName;
        @BindView(R.id.tvDes)
        TextView tvDes;
        @BindView(R.id.tvGet)
        TextView tvGet;

        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
    public static interface GetListener{
        void getRainbow(DayTaskData data);
    }
}
